package com.enjoyor.health.HGPT.core.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.enjoyor.health.HGPT.core.dto.UserWatchConfigDto;
import com.enjoyor.health.HGPT.core.entity.UserInfoApp;
import com.enjoyor.health.HGPT.core.entity.UserInfoMiniapp;
import com.enjoyor.health.HGPT.core.mapper.UserInfoAppMapper;
import com.enjoyor.health.HGPT.core.service.IUserInfoAppService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 小程序用户表 服务实现类
 * </p>
 *
 * @author enjoyor
 * @since 2023-12-19
 */
@Service
public class UserInfoAppServiceImpl extends ServiceImpl<UserInfoAppMapper, UserInfoApp> implements IUserInfoAppService {

    @Override
    public UserInfoApp getByPhone(String phone) {
        QueryWrapper<UserInfoApp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public UserInfoApp getByUserId(String userId) {
        QueryWrapper<UserInfoApp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public int updateConfig(UserWatchConfigDto dto) {
        LambdaUpdateWrapper<UserInfoApp> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserInfoApp::getUserId, dto.getUserId())
            .set(UserInfoApp::getConfig, JSONObject.toJSONString(dto));
        return baseMapper.update(null, updateWrapper);
    }
}
